<template>
  <a-dropdown placement="bottomRight" v-model="show">
    <span class="header-notice">
      <a-badge class="notice-badge" :count="count">
        <a-icon :class="['header-notice-icon']" type="bell" />
      </a-badge>
    </span>
    <div slot="overlay" style="box-shadow: 0 0 15px 1px rgb(0, 0, 0, 0.15);">
      <a-spin :spinning="loading">
        <a-tabs class="dropdown-tabs" :tabBarStyle="{textAlign: 'center'}" @change="onChange">
          <a-tab-pane tab="未读" key="1"></a-tab-pane>
          <a-tab-pane tab="已读" key="2"></a-tab-pane>
        </a-tabs>
        <a-list :loading="listLoading" class="tab-pane">
          <a-list-item v-for="item in messageList" :key="item.id">
            <a-button slot="extra" type="link" @click="$router.push({ name: 'MessageDetail', params: { id: item.id } })">详情</a-button>
            <a-list-item-meta :title="item.title"></a-list-item-meta>
          </a-list-item>
        </a-list>
      </a-spin>
    </div>
  </a-dropdown>
</template>

<script>
import { mapActions } from 'vuex'

export default {
  props: {
    count: {
      type: Number,
      default: 0
    }
  },
  name: 'MessageDropdown',
  data () {
    return {
      loading: false,
      show: false,
      queryParam: {},
      messageList: [],
      listLoading: false
    }
  },
  created () {
    this.onGetMessage()
  },
  methods: {
    ...mapActions(['GetMessage']),
    onGetMessage () {
      this.listLoading = true
      const { GetMessage } = this
      this.queryParam['filter[type]'] = '1'
      this.queryParam['page_size'] = 7
      GetMessage(this.queryParam).then(res => {
        if (res.status === 0) {
          this.messageList = res.result.data
          this.listLoading = false
        } else {
          this.$warning({
            title: res.msg
          })
          this.listLoading = false
        }
      })
    },
    onChange (key) {
      this.listLoading = true
      const { GetMessage } = this
      this.queryParam['filter[type]'] = key
      GetMessage(this.queryParam).then(res => {
        if (res.status === 0) {
          this.messageList = res.result.data
          this.listLoading = false
        } else {
          this.$warning({
            title: res.msg
          })
          this.listLoading = false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .header-notice{
    display: inline-block;
    transition: all 0.3s;
    span {
      vertical-align: initial;
    }
    .notice-badge{
      color: inherit;
      .header-notice-icon{
        font-size: 16px;
        padding: 4px;
      }
    }
  }
  .dropdown-tabs{
    background-color: #fff;
    box-shadow: 0 2px 8px #eee;
    border-radius: 4px;
  }
  .tab-pane{
    background-color: #fff;
    padding: 0 24px 12px;
    min-width:400px;
    min-height: 250px;
  }
  /deep/ .ant-dropdown-content {
    width: 400px;
  }
</style>
